<script>
	import { useAuthStore } from './stores';
	
	export default {
		// 访问地址demo:
		// 直播复制:https://userh5.ruijinsy.com/pages/live/index?live_no=live1&company_code=qiyebianhao&mid=1
		// 会员邀请:https://userh5.ruijinsy.com/pages/live/vip?company_code=qiyebianhao&mid=1
		onLaunch: function() {
			
			const params = new URLSearchParams(window.location.search)
			const openid = params.get('openid') || uni.getStorageSync('openid')
			const localComp = uni.getStorageSync('companyCode')//本地数据
			const urlComp = params.get('company_code')		//链接数据
			
			if(params.get('openid')){
				uni.setStorageSync('openid',params.get('openid'))
				localComp = uni.setStorageSync('companyCode',urlComp)
			}
			if(openid){
				if(localComp && localComp !== urlComp){
					this.goWxAuth()
				}else{
					this.getUserInfo()
				}
			}else{
				this.goWxAuth()
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			goWxAuth(){
				return
				false
				const url = window.location.href
				const orgin = url.split('?')[0]
				const source = url.split('?')[1]
				window.location.href = `https://h5.whalesaas.com?${source}&orgin=${orgin}`
				// window.location.href = `https://h5-test.whalesaas.com?${source}&orgin=${orgin}`
			},
			//弃用
			getUserBycode(){
				const params = new URLSearchParams(window.location.search)
				const code = params.get('code')
				console.log('code=',code)
				uni.request({
					url: `${this.$baseURL}/api/toc/auth/wechat/getAccessTokenByCode?code=${code}`,
					method: "GET",
					success: (res) => {
						if(res.data.code === 200001){
							const useData = useAuthStore()
							const companyCode = params.get('company_code')
							let token = res.data.data.access_token
							useData.setToken(token)
							useData.setCode(companyCode)
							useData.setNickName(res.data.data.customer.wechat_nickname)
							useData.setAvatar(res.data.data.customer.avatar_url)
							useData.setOpenid(res.data.data.customer.wechat_openid)
							uni.setStorageSync('shortToken',code)
						}else{
							console.log(res.data.message)
						}
					}
				})
			},
			getUserInfo(){
				const params = new URLSearchParams(window.location.search)
				const openid = params.get('openid') || uni.getStorageSync('openid')
				const companyCode = params.get('company_code') || uni.getStorageSync('companyCode')
				const mid = params.get('mid')
				
				uni.request({
					url: `${this.$baseURL}/api/toc/customer/detail?mid=${mid}&openid=${openid}`,
					method: "GET",
					header:{ 'CompanyCode': companyCode },
					success: (res) => {
						if(res.data.code === 200001){
							const useData = useAuthStore()
							useData.setNickName(res.data.data.customer_info.wechat_nickname)
							useData.setAvatar(res.data.data.customer_info.avatar_url)
							useData.setOpenid(res.data.data.customer_info.wechat_openid)
							uni.setStorageSync('companyCode',companyCode)
						}else{
							console.log('getUserInfo error',res.data.message)
							uni.showToast({
								icon: 'none',
								title: res.data.message
							})
						}
					},
					fail: (err) => {
						console.log('getUserInfo fail',err)
					}
				})
			}
		}
	}
</script>

<style>
	/* .mplayer-footer{display: none!important;} */
	/*每个页面公共css */
	.live-mui .mplayer-header,
	.live-mui .mplayer-footer{display: none!important;}
	.live-mui .mini-progress{
		opacity: 0!important;
	}
</style>